*DO-FILE for 
*Van Ryzin, G.G. (2021)
*The perceived fairness of active representation: Evidence from a survey experiment 
*Public Administration Review.

********************************************************************************
*BEGIN DO-FILE
********************************************************************************

*Drop if terminated (because of age, focus, over-quota) plus 22 speeders
encode term_0, generate(term_0n)
keep if term_0n==.

*Demographic profile
gen Q1_4C = Q1_4
recode Q1_4C (18/24=1)(25/44=2)(45/59=3)(60/74=4)(75/100=5)

label define Q1_4Clabels 1 "18-24" 2 "25-44" 3 "45-59" 4 "60-74" 5 "75+"
label values Q1_4C Q1_4Clabels

tab Q1_3
tab Q1_4C
tab Q1_5
tab Q1_6
tab Q15_8
tab Q15_10
tab Q15_11

gen leftright = Q15_11
recode leftright (1/5=0)(6/10=1)
gen edulevel = Q15_8
recode edulevel (1/4=0)(5/7=1)

*Genderate experimental factor (teaching vignette)

gen teacher=.
recode teacher (.=1) if Teacher_student_DO_Q2_1==1
recode teacher (.=2) if Teacher_student_DO_Q2_2==1
recode teacher (.=3) if Teacher_student_DO_Q2_3==1
recode teacher (.=4) if Teacher_student_DO_Q2_4==1
recode teacher (.=5) if Teacher_student_DO_Q2_5==1

label define teacherL 1 "Male teacher, male student" 2 "Female teacher, male student" 3 "Female teacher, male student" 4 "Female teacher, female student" 5 "No-gender control"
label values teacher teacherL
tab teacher
quietly tabulate teacher, generate (teacher_)

*Genderate experimental factor (policing vignette)

gen police=.
recode police (.=1) if Police_citizen_DO_Q3_1==1
recode police (.=2) if Police_citizen_DO_Q3_2==1
recode police (.=3) if Police_citizen_DO_Q3_3==1
recode police (.=4) if Police_citizen_DO_Q3_4==1
recode police (.=5) if Police_citizen_DO_Q3_5==1

label define policeL 1 "White officer, white citizen" 2 "Black officer, white citizen" 3 "White officer, black citizen" 4 "Black officer, black citizen" 5 "No-race control"
label values police policeL
tab police
quietly tabulate police, generate (police_)

*Balance tests
tabulate Q1_3 teacher, chi2 column
tabulate Q1_3 police, chi2 column
tabulate Q1_4C teacher, chi2 column
tabulate Q1_4C police, chi2 column
tabulate Q1_5 teacher, chi2 column
tabulate Q1_5 police, chi2 column
tabulate Q1_6 teacher, chi2 column
tabulate Q1_6 police, chi2 column

*Reverse coding of DVs
gen Q2_6r = 6 - Q2_6
gen Q3_6r = 6 - Q3_6

*Oneway ANOVA
mean Q2_6r, over (teacher)
oneway Q2_6r teacher
mean Q3_6r, over(police)
oneway Q3_6r police

*Dummy regressions and coefficient plots
reg Q2_6r teacher_1 teacher_2 teacher_3 teacher_4, level(90)
estimate store Total
reg Q2_6r teacher_1 teacher_2 teacher_3 teacher_4 if Q1_3==1, level(90)
estimate store Women
reg Q2_6r teacher_1 teacher_2 teacher_3 teacher_4 if Q1_3==2 , level(90)
estimate store Men
coefplot (Total, label(Total)) (Women, label(Women)) (Men, label(Men)), drop(_cons) xline(0) levels(90)

reg Q3_6r police_1 police_2 police_3 police_4, level(90)
estimate store Total
reg Q3_6r police_1 police_2 police_3 police_4 if Q1_5==1, level(90)
estimate store White
reg Q3_6r police_1 police_2 police_3 police_4 if Q1_5==2, level(90)
estimate store Black
reg Q3_6r police_1 police_2 police_3 police_4 if Q1_5==3, level(90)
estimate store Hispan
coefplot (Total, label(Total)) (White, label(White)) (Black, label(Black)) (Hispan, label(Hispanic)), drop(_cons) xline(0) levels(90)

*Two-way ANOVA to test if effects differ across groups
tab Q1_3
anova Q2_6r teacher##Q1_3
tab Q1_5
gen Q1_5wbh = Q1_5
recode Q1_5wbh (4=.)(5=.)
anova Q3_6r police##Q1_5wbh

*Regression analysis by political ideology
bysort leftright: reg Q2_6r teacher_1 teacher_2 teacher_3 teacher_4, level(90)
bysort leftright: reg Q3_6r police_1 police_2 police_3 police_4, level(90)

*Alternative codings of the treatments for 2 x 2 regressions (requested by reviewer)
*Note: main effect (-.5, .5) coding used to generate ANOVA-style main effects in regression framework

gen femteach = .
recode femteach (.=-.5) if teacher_1==1
recode femteach (.=.5) if teacher_2==1
recode femteach (.=-.5) if teacher_3==1
recode femteach (.=.5) if teacher_4==1

gen femstudent = .
recode femstudent (.=-.5) if teacher_1==1
recode femstudent (.=-.5) if teacher_2==1
recode femstudent (.=.5) if teacher_3==1
recode femstudent (.=.5) if teacher_4==1

reg Q2_6r c.femteach##c.femstudent
bysort Q1_3: reg Q2_6r c.femteach##c.femstudent
*to test if the effects differ across groups
anova Q2_6r c.femteach##c.femstudent##Q1_3

gen whitecop = .
recode whitecop (.=.5) if police_1==1
recode whitecop (.=-.5) if police_2==1
recode whitecop (.=.5) if police_3==1
recode whitecop (.=-.5) if police_4==1

gen whitemot = .
recode whitemot (.=.5) if police_1==1
recode whitemot (.=.5) if police_2==1
recode whitemot (.=-.5) if police_3==1
recode whitemot (.=-.5) if police_4==1

reg Q3_6r c.whitecop##c.whitemot
bysort Q1_5: reg Q3_6r c.whitecop##c.whitemot
*to test if the effects differ across groups
anova Q3_6r c.whitecop##c.whitemot##Q1_5


********************************************************************************
*END DO-FILE
********************************************************************************
